﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Update
{
    public class TasksUpdateHandler : IRequestHandler<TasksUpdateCommon, Result>
    {
        private readonly IBaseRepository<ProductionTasks> ProductionTasksRepository;
        public IMapper mapper { get; set; }
        public TasksUpdateHandler(IBaseRepository<ProductionTasks> productionTasksRepository, IMapper mapper)
        {
            ProductionTasksRepository = productionTasksRepository;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(TasksUpdateCommon request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<ProductionTasks>(request);
            string sql = "UPDATE ProductionTasks SET TasksCode=@TasksCode,TasksName=@TasksName ,TasksType=@TasksType ,TasksState=@TasksState " +
                         ",ChooseId=@ChooseId ,MaterialSpecsId=@MaterialSpecsId,ItemUnitId=@ItemUnitId,ProductionNum=@ProductionNum,StartTime=@StartTime,CompletionTime=@CompletionTime,ItemManifestCode=@ItemManifestCode, ItemManifestVersion=@ItemManifestVersion," +
                         " ProcessRouteNo=@ProcessRouteNo, ProcessVersion=@ProcessVersion,InboundQuantity=@InboundQuantity," +
                         " NumberReturns=@NumberReturns,Peole=@Peole,DepartmentId=@DepartmentId,Handle=@Handle,HandleContext=@HandleContext,HandleTime=@HandleTime, Pid=@Pid,IsDelete=@IsDelete WHERE TasksId =@TasksId";
            var list = ProductionTasksRepository.Update(sql, new
            {
                TasksId = AddData.TasksId,
                TasksCode = AddData.TasksCode,
                TasksName = AddData.TasksName,
                TasksType = AddData.TasksType,
                TasksState = AddData.TasksState,
                ChooseId = AddData.ChooseId,

                MaterialSpecsId = AddData.MaterialSpecsId,
                ItemUnitId = AddData.ItemUnitId,
                ProductionNum = AddData.ProductionNum,
                StartTime = AddData.StartTime,
                CompletionTime = AddData.CompletionTime,
                ItemManifestCode = AddData.ItemManifestCode,

                ItemManifestVersion = AddData.ItemManifestVersion,
                ProcessRouteNo = AddData.ProcessRouteNo,
                ProcessVersion = AddData.ProcessVersion,
                InboundQuantity = AddData.InboundQuantity,
                NumberReturns = AddData.NumberReturns,
                Peole = AddData.Peole,

                DepartmentId = AddData.DepartmentId,
                Handle = AddData.Handle,
                HandleContext = AddData.HandleContext,
                HandleTime = AddData.HandleTime,
                Pid = AddData.Pid,
                IsDelete = AddData.IsDelete
            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }
            return results;
        }
    }
}
